home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / demos / GL / snurb / rotobj.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  3.9 KB  |  183 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #include <gl.h>
  18. #include <device.h>
  19. #include <math.h>
  20. #include <stdio.h>
  21. #include "defines.h"
  22. #include "event.h"
  23. #include "trackball.h"
  24. #include "rotobj.h"
  25.  
  26.  
  27. static void add_childrens_center(
  28.     struct node_struct *child,
  29.     Coord total[3],
  30.     int *counter);
  31.  
  32. static void total_selected_objects(
  33.     struct node_struct *child,
  34.     Coord total[3],
  35.     int *counter);
  36.  
  37.  
  38.     
  39. extern Matrix view_matrix;
  40. extern Matrix R_view_matrix;
  41. extern Matrix identity_matrix;
  42.  
  43. extern struct node_struct *root;
  44.  
  45. /* About the window... */ 
  46. extern long origin_x, origin_y, size_x, size_y;
  47.  
  48. /* Mouse values in screen coords */
  49. static Scoord mx, my;
  50.     
  51. /* Scaled mouse values (-1.0 to 1.0) */
  52. static Coord last_x, last_y, new_x, new_y;
  53.  
  54. static Coord x_axis[3] = {1.0, 0.0, 0.0};
  55. static Coord rotation_center[3];
  56.     
  57.  
  58. void rotate_object()
  59. {
  60.     mx = getvaluator(MOUSEX) - origin_x;
  61.     my = getvaluator(MOUSEY) - origin_y;
  62.  
  63.     /* Scale mouse values to square around -1.0 to 1.0  */
  64.     last_x = 2.0*((Coord)mx/size_x - 0.5);
  65.     last_y = 2.0*((Coord)my/size_y - 0.5);
  66.  
  67.     compute_center(rotation_center);
  68. }
  69.  
  70.  
  71. void update_object_rotation(Matrix object_matrix)
  72. {
  73.     Coord new_euler[4];
  74.     Coord R_euler[4]; /* reversed */
  75.     Matrix new_object_matrix;
  76.     
  77.     mx = getvaluator(MOUSEX) - origin_x;
  78.     my = getvaluator(MOUSEY) - origin_y;
  79.     
  80.     /* Scale mouse values to square around -1.0 to 1.0  */
  81.     new_x = 2.0*((Coord)mx/size_x - 0.5);
  82.     new_y = 2.0*((Coord)my/size_y - 0.5);
  83.  
  84.     axis_to_euler(x_axis, 0.0, new_euler);
  85.     trackball(new_euler, last_x, last_y, new_x, new_y);
  86.  
  87.     last_x = new_x;
  88.     last_y = new_y;
  89.     
  90.     build_rotmatrix(new_object_matrix, new_euler);
  91.  
  92.     loadmatrix(identity_matrix);
  93.         
  94.     translate(rotation_center[0],rotation_center[1],rotation_center[2]); 
  95.     multmatrix(R_view_matrix);
  96.     multmatrix(new_object_matrix);
  97.     multmatrix(view_matrix);
  98.     translate(-rotation_center[0],-rotation_center[1],-rotation_center[2]); 
  99.  
  100.     getmatrix(object_matrix);
  101. }
  102.  
  103.  
  104.  
  105. static void add_childrens_center(
  106.     struct node_struct *child,
  107.     Coord total[3],
  108.     int *counter)
  109. {
  110.     int s,t,c;
  111.     
  112.     if (child != NULL)
  113.     {
  114.         if (child->node_type == PATCH)
  115.         {
  116.             for (s=0; s<NUMPOINTS; s++)
  117.                 for (t=0; t<NUMPOINTS; t++)
  118.                     for (c=0; c<3; c++)
  119.                         total[c] += child->patch->control[s][t][c];
  120.  
  121.             *counter += NUMPOINTS * NUMPOINTS;
  122.         }
  123.         
  124.         add_childrens_center(child->child, total, counter);
  125.         add_childrens_center(child->sibling, total, counter);
  126.     }
  127. }
  128.  
  129.  
  130.  
  131. static void total_selected_objects(
  132.     struct node_struct *child,
  133.     Coord total[3],
  134.     int *counter)
  135. {
  136.     int s,t,c;
  137.     
  138.     if (child != NULL)
  139.     {
  140.         if (child->selected)
  141.         {
  142.             if (child->node_type == PATCH)
  143.             {
  144.                 for (s=0; s<NUMPOINTS; s++)
  145.                     for (t=0; t<NUMPOINTS; t++)
  146.                         for (c=0; c<3; c++)
  147.                             total[c] += child->patch->control[s][t][c];
  148.  
  149.                 *counter += NUMPOINTS * NUMPOINTS;
  150.             }
  151.             else
  152.                 add_childrens_center(child->child, total, counter);
  153.         }
  154.  
  155.         total_selected_objects(child->sibling, total, counter);
  156.     }
  157. }
  158.         
  159.  
  160.  
  161.  
  162. /* Computes the center as the average of all control points of selected
  163.  * objects
  164.  */
  165. void compute_center(Coord center[3])
  166. {
  167.     Coord total[3];
  168.     int s,t,c, counter = 0;
  169.  
  170.     total[0] = total[1] = total[2] = (Coord) 0.0;
  171.  
  172.     total_selected_objects(root->child, total, &counter);
  173.  
  174.     if (counter)
  175.         for(c=0; c<3; c++)
  176.             center[c] = total[c]/(Coord)counter;
  177. }
  178.  
  179.  
  180.  
  181.  
  182.  
  183.